/*
 * Copyright 2009, 2010, YUNNAN Co-soft Information Technology Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 * Description: Contact us.
 * Version: 1.0.0.3, May 18, 2010
 * Author: Gang Liu
 * Author: Liang Ding
 */
function sendMail(){
    var cName = $("#contactorName").val();
    var email = $("#contactorEmailAddress").val();
    var phone = $("#contactorPhoneNumber").val();
    var secCode = $("#capCode").val();
    var message = $("#contactorMessage").val();

    var divWidth = document.body.clientWidth;
    var divHight = document.body.clientHeight;

    var requestJSONObject = {
        "contactorName" : cName,
        "contactorEmailAddress" : email,
        "contactorPhoneNumber" : phone,
        "capCode" : secCode,
        "contactorMessage" : message
    }
    $("#watting").show();
    $("#watting-page").css({
        "width":divWidth,
        "height":divHight
    });
   
    jsonRpc.mailService.sendMail(sendMailCallback, requestJSONObject);
}

function clearContactUsFields(){
    $("#contactorName").val("");
    $("#contactorEmailAddress").val("");
    $("#contactorPhoneNumber").val("");
    $("#capCode").val("");
    $("#contactorMessage").val("");    
}

function clearContactUsHints(){
    $("#nameCannotBeNull").hide();
    $("#emailCannotBeNull").hide();
    $("#messageCannotBeNull").hide();
    $("#capCodeCannotBeNull").hide();
    $("#capcodeIncorrect").hide();
    $("#illegalEmailAddress").hide();
    $("#mailSent").hide();
    $("#mailSentFail").hide();
}

function sendMailCallback(result, error){
    if (error) {
        alert("Error: " + JSON.stringify(error));

        return;
    }

    $("#watting").hide();
    clearContactUsHints();
    var isValid = true;
    if (result.msgs.nameCannotBeNull) {
        $("#nameCannotBeNull").text(result.msgs.nameCannotBeNull);
        $("#nameCannotBeNull").show();
        isValid = false;
    }

    if(result.msgs.emailCannotBeNull){
        $("#emailCannotBeNull").text(result.msgs.emailCannotBeNull);
        $("#emailCannotBeNull").show();
        isValid = false;
    } else {
        if(result.msgs.illegalEmailAddress){
            $("#illegalEmailAddress").text(result.msgs.illegalEmailAddress);
            $("#illegalEmailAddress").show();
            $("#contactorEmailAddress").val("");
            isValid = false;
        }
    }
    if(result.msgs.messageCannotBeNull){
        $("#messageCannotBeNull").text(result.msgs.messageCannotBeNull);
        $("#messageCannotBeNull").show();
        isValid = false;
    }
    if(result.msgs.capCodeCannotBeNull){
        $("#capCodeCannotBeNull").text(result.msgs.capCodeCannotBeNull);
        $("#capCodeCannotBeNull").show();        
        isValid = false;
    } else {
        if(result.msgs.capcodeIncorrect){
            $("#capcodeIncorrect").text(result.msgs.capcodeIncorrect);
            $("#capcodeIncorrect").show();
            $("#capCode").val("");
            isValid = false;
        }
    }

    // alert(result.sc);
    if (isValid) {
        if ("EMAIL_SENT_FAIL_" === result.sc) {
            $("#mailSentFail").text(result.msgs.mailSentFail);
            $("#mailSentFail").show();
        } else {
            $("#mailSent").text(result.msgs.mailSent);
            $("#mailSent").show();
        }
        clearContactUsFields();
    } else {
        
    }
    
    $("#cap-code-image").click();
}

//function sendMailCallBack(data){
//    $("#watting").hide();
//
//    if(data.serviceFeedBack.length == 0){
//        $("#error-content").css("color", "white");
//        $("#error-content").css("font-size", "13px");
//        $("#error-content").append("    "+data.mailSendStatus);
//
//        clearContactUsHints();
//    }else {
//        $("#error-content").css("color", "red");
//        $("#error-content").css("font-size", "12px");
//        for(var i=0;i < data.serviceFeedBack.length;i++){
//            var message = "* " + data.serviceFeedBack[i];
//            if(i%2 == 1){
//                message += "<br>";
//            }
//            $("#error-content").append(message);
//        }
//        <!--onclick="this.src=this.src+'?'" -->
//    }
//}

function changeCode(image)
{
    var source = image.src;
    if(($.browser.msie) && ($.browser.version == "7.0"))
    {
        image.src="";
    }
    image.src = source+'?';
}